import React, {useCallback, useEffect} from "react";
import Noauth from '@/app/noauth'
import {usePathname} from "next/navigation";
import {useSelector} from "react-redux";
import {RootState} from "@/stores";
import {findAllMenuItemByPath} from "../../config/menu";
import ACCESS_ENUM from "@/access/accessEnums";
import checkAccess from "@/access/checkAccess";

/**
 * 全局权限统一校验拦截器
 * @param children
 * @constructor
 */
const AccessLayout:React.FC<Readonly<{
    children: React.ReactNode;
}>> = ({children}) => {
    const pathName = usePathname()
    // 当前登录的用户
    const loginUser = useSelector((state: RootState) => state.loginUser)
    // 获取当前路径需要的权限
    const menu = findAllMenuItemByPath(pathName)
    const needAccess = menu?.access ?? ACCESS_ENUM.NOT_LOGIN
    // 权限校验
    const canAccess = checkAccess(loginUser, needAccess)
    if (!canAccess){
        return <Noauth></Noauth>
    }
    return children;
}

export default AccessLayout;
